package calPoints;
import java.util.Stack;

public class Solution {
    public int calPoints(String[] operations) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < operations.length; i++) {
            String c = operations[i];
            if(c.equals("D")) {
             stack.push(stack.peek()*2);
            }else if(c.equals("D")) {
                stack.pop();
            }else if(c.equals("+")) {
                int lastOne = stack.pop();
                int curr = lastOne+stack.peek();
                stack.push(lastOne);
                stack.push(curr);
            }else {
                stack.push(Integer.parseInt(c));
            }
        }
        int ans = 0;
        for (int v: stack) {
            ans+=v;
        }
        return ans;
    }
}
